package com.cupk.rotaryguard.user.controller;

import com.cupk.common.base.utils.result.Result;
import com.cupk.rotaryguard.user.entity.vo.UserVo;
import com.cupk.rotaryguard.user.entity.vo.LoginUserVo;
import com.cupk.rotaryguard.user.service.UserService;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author tjy
 * @date 2023/3/17
 * @apiNote
 */
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 登陆
     *
     * @param userVo userVo
     * @return login
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginUserVo userVo, HttpServletRequest request) {
        return userService.login(userVo, request);
    }

    /**
     * 登出
     *
     * @param request request
     * @return 返回
     */
    @RequestMapping("/logout")
    public Result logout(HttpServletRequest request) {
        return userService.logout(request);
    }

    /**
     * 根据用户id查询用户信息
     *
     * @param id id
     * @return 返回
     */
    @GetMapping("/{id}")
    public Result getUserInfoById(@PathVariable Long id) {
        return userService.getUserInfoById(id);
    }

    /**
     * 添加用户
     *
     * @return 返回
     */
    @PostMapping("/add")
    public Result addUser(@RequestBody UserVo userVo) {
        return userService.addUser(userVo);
    }

    /**
     * 修改密码
     *
     * @param userId
     * @param oldPwd
     * @param newPwd
     * @return
     */
    @PostMapping("/chPwd")
    public Result chPwd(Long userId, String oldPwd, String newPwd) {
        return userService.chPwd(userId, oldPwd, newPwd);
    }

    /**
     * 修改用户名
     *
     * @param userId
     * @param username
     * @return
     */
    @PostMapping("/chUsername")
    public Result chUsername(Long userId, String username) {
        return userService.chUsername(userId, username);
    }

    /**
     * 单个删除用户（逻辑删除）
     *
     * @param id id
     * @return 返回
     */
    @PostMapping("/delUser/{id}")
    public Result delUserById(@PathVariable Integer id) {
        return userService.delUserById(id);
    }

    /**
     * 批量删除用户（逻辑删除）
     *
     * @param ids id列表
     * @return 返回
     */
    @PostMapping("/delUsers")
    public Result delUsers(@RequestBody(required = false) List<Integer> ids) {
        return userService.delUsers(ids);
    }

    /**
     * 用户列表
     */
    @GetMapping("/list")
    public Result userList() {
        return userService.listUserInfo();
    }

    /**
     * 根据用户名分页查询
     *
     * @param pageNum  pageNum
     * @param pageSize pageSize
     * @param username username
     * @return 返回
     */
    @GetMapping("/list-username")
    public Result getUsersByUsername(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(defaultValue = "") String username) {
        return userService.getUsersByUsername(pageNum, pageSize, username);
    }

    /**
     * 获取被注销的用户
     *
     * @return 返回
     */
    @GetMapping("/list-user-banned")
    public Result listBannedUsers(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam Integer pageSize) {
        return userService.listBannedUsers(pageNum, pageSize);
    }

    /**
     * 封禁账户
     *
     * @param id id
     * @return 返回
     */
    @PostMapping("/ban-user")
    public Result banUser(@RequestParam Integer id) {
        return userService.banUser(id);
    }

    /**
     * 更改用户信息
     *
     * @param id
     * @param username
     * @param tel
     * @param address
     * @param email
     * @return
     */
    @PostMapping("/ch-userinfo/")
    public Result chUserInfo(@RequestParam String id,
                             @RequestParam String username,
                             @RequestParam String tel,
                             @RequestParam String address,
                             @RequestParam String email) {
        return userService.chUserInfo(id, username, tel, address, email);
    }
}
